package com.datagateway.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 异步任务配置类
 * 配置线程池以支持高并发数据处理
 * 
 * @author Data Gateway Team
 * @version 1.0.0
 */
@Configuration
public class AsyncConfig implements AsyncConfigurer {

    @Autowired
    private DataGatewayProperties properties;

    /**
     * 配置异步任务执行器
     * 
     * @return 线程池执行器
     */
    @Override
    @Bean(name = "taskExecutor")
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        
        // 设置核心线程数
        executor.setCorePoolSize(properties.getPerformance().getCorePoolSize());
        
        // 设置最大线程数
        executor.setMaxPoolSize(properties.getPerformance().getMaxPoolSize());
        
        // 设置队列容量
        executor.setQueueCapacity(properties.getPerformance().getQueueCapacity());
        
        // 设置线程空闲时间
        executor.setKeepAliveSeconds(properties.getPerformance().getKeepAliveSeconds());
        
        // 设置线程名前缀
        executor.setThreadNamePrefix("DataGateway-Async-");
        
        // 设置拒绝策略：由调用线程执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        
        // 等待所有任务结束后再关闭线程池
        executor.setWaitForTasksToCompleteOnShutdown(true);
        
        // 等待时间
        executor.setAwaitTerminationSeconds(60);
        
        // 初始化线程池
        executor.initialize();
        
        return executor;
    }

    /**
     * 配置数据处理专用线程池
     * 
     * @return 数据处理线程池
     */
    @Bean(name = "dataProcessingExecutor")
    public Executor dataProcessingExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        
        // 数据处理线程池配置
        executor.setCorePoolSize(20);
        executor.setMaxPoolSize(100);
        executor.setQueueCapacity(2000);
        executor.setKeepAliveSeconds(60);
        executor.setThreadNamePrefix("DataProcessing-");
        
        // 使用丢弃最老任务的策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
        
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);
        executor.initialize();
        
        return executor;
    }

    /**
     * 配置Hive写入专用线程池
     * 
     * @return Hive写入线程池
     */
    @Bean(name = "hiveWriteExecutor")
    public Executor hiveWriteExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        
        // Hive写入线程池配置（较小，避免过多并发写入）
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(20);
        executor.setQueueCapacity(500);
        executor.setKeepAliveSeconds(60);
        executor.setThreadNamePrefix("HiveWrite-");
        
        // 使用阻塞策略，确保数据不丢失
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(120);
        executor.initialize();
        
        return executor;
    }
}
